package org.example.demo3.demos.web.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

@Data
@ApiModel(value = "接口返回对象", description = "接口返回对象")
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 返回处理消息
     */
    @ApiModelProperty(value = "返回处理消息")
    private String message = ResponseEnum.SUCCESS.getMsg();

    /**
     * 返回代码
     */
    @ApiModelProperty(value = "返回代码")
    private String code = ResponseEnum.SUCCESS.getCode();

    /**
     * 返回数据对象 data
     */
    @ApiModelProperty(value = "返回数据对象")
    private T data;

    /**
     * 时间戳
     */
    @ApiModelProperty(value = "时间戳")
    private long timestamp = System.currentTimeMillis();

    public Result() {

    }

    /**
     * 无返回值的操作成功
     * @return
     * @param <T>
     */
    public static <T> Result<T> OK() {
        Result<T> r = new Result<T>();
        r.setCode(ResponseEnum.SUCCESS.getCode());
        r.setMessage(ResponseEnum.SUCCESS.getMsg());
        return r;
    }

    /**
     * 有返回值的操作成功
     * @param data
     * @return
     * @param <T>
     */
    public static <T> Result<T> OK(T data) {
        Result<T> r = new Result<T>();
        r.setCode(ResponseEnum.SUCCESS.getCode());
        r.setMessage(ResponseEnum.SUCCESS.getMsg());
        r.setData(data);
        return r;
    }

    /**
     * 成功code返回2000，前端取message进行提示
     * @param message
     * @return
     * @param <T>
     */
    public static <T> Result<T> OK_MSG(String message) {
        Result<T> r = new Result<T>();
        r.setCode(ResponseEnum.SUCCESS_MSG.getCode());
        r.setMessage(message);
        return r;
    }

    /**
     * 未知异常，全局异常处理的 Exception error使用
     * 业务逻辑中避免使用
     * @return
     * @param <T>
     */
    public static <T> Result<T> ERROR() {
        Result<T> r = new Result<T>();
        r.setCode(ResponseEnum.ERROR.getCode());
        r.setMessage(ResponseEnum.ERROR.getMsg());
        return r;
    }

    /**
     * 透传调研接口的异常，code和msg都是透传
     * 不得代码中 自定义 code和 msg
     * @param code
     * @param msg
     * @return
     */
    public static Result<Object> ERROR(String code, String msg) {
        Result<Object> r = new Result<Object>();
        r.setCode(code);
        r.setMessage(msg);
        return r;
    }

    /**
     * 透传调研接口的异常，code和msg都是透传
     * 不得代码中 自定义 code和 msg
     * @param code
     * @param msg
     * @return
     */
    public static <T> Result<T> ERRORT(String code, String msg) {
        Result<T> r = new Result<T>();
        r.setCode(code);
        r.setMessage(msg);
        return r;
    }
    /**
     *
     * @param msg
     * @return
     */
    public static Result<Object> ERROR(String msg) {
        Result<Object> r = new Result<Object>();
        r.setCode(ResponseEnum.BUSINESS_ERROR.getCode());
        r.setMessage(msg);
        return r;
    }

    /**
     * 统一异常处理参数校验，业务代码不得使用（需要手动校验参数的除外）
     * @param msg
     * @return
     * @param <T>
     */
    public static <T> Result<T> ValidateError(String msg) {
        Result<T> r = new Result<T>();
        r.setCode(ResponseEnum.PARAM_VALIDATED_ERROR.getCode());
        r.setMessage(msg);
        return r;
    }

    /**
     * 返回自定义异常，有返回值
     * @param responseEnum
     * @param data
     * @return
     * @param <T>
     */
    public static <T> Result<T> ERROR(ResponseEnum responseEnum, T data) {
        Result<T> r = new Result<T>();
        r.setCode(responseEnum.getCode());
        r.setMessage(responseEnum.getMsg());
        r.setData(data);
        return r;
    }

    /**
     * 返回自定义异常，无返回值
     * @param responseEnum
     * @return
     * @param <T>
     */
    public static <T> Result<T> ERROR(ResponseEnum responseEnum) {
        Result<T> r = new Result<T>();
        r.setCode(responseEnum.getCode());
        r.setMessage(responseEnum.getMsg());
        return r;
    }
}

